package com.ruoyi.framework.web.service;

import cn.hutool.core.util.StrUtil;
import com.lx.ast.annotated.Describe;
import com.lx.ast.annotated.Relation;
import com.lx.ast.service.ExchangeService;
import com.lx.information.mapper.InformationJointCaseMapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author liuzhiqiang
 */
@Service
public class ExchangeServiceImpl implements ExchangeService {

    public final static String TYPE = "type";

    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private InformationJointCaseMapper informationJointCaseMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String relationHandle(Relation relation, Object value) {
        if (value != null) {
            String sql = "select " + relation.showField() + " from " + relation.table() + " where " + relation.targetField() + " = '" + value + "'";
            String label = informationJointCaseMapper.execute(sql);
            return label;
        }
        return "";
    }

    @Override
    public String describeHandle(Describe describe, Object value) {
        if (value != null && value instanceof String) {
            if (describe.type().equals(TYPE)) {
                // 数据字典分类
                return getNameByType((String) value);
            } else {
                // 数据字典
                return getLabelByValue((String) value);
            }
        }
        return "";
    }

    public String getNameByType(String type) {
        String key = "dictType_" + type;
        String name = redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(name)) {
            return name;
        } else {
            synchronized (key.intern()) {
                SysDictType sysDictType = dictTypeService.selectDictTypeByType(type);
                if (sysDictType != null) {
                    name = sysDictType.getDictName();
                } else {
                    name = "";
                }
                redisTemplate.opsForValue().set(key, name, 1800, TimeUnit.SECONDS);
                return name;
            }
        }
    }

    public String getLabelByValue(String value) {
        String key = "dictData_" + value;
        String label = redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(label)) {
            return label;
        } else {
            synchronized (key.intern()) {
                SysDictData sysDictData = dictDataService.selectDictDataByValue(value);
                if (sysDictData != null) {
                    label = sysDictData.getDictLabel();
                } else {
                    label = "";
                }
                redisTemplate.opsForValue().set(key, label, 1800, TimeUnit.SECONDS);
                return label;
            }
        }
    }


}
